/**
 * Settle模块 - 对账管理API
 * @description 对账管理相关的API接口定义
 * @author AI Assistant
 * @since 1.0.0
 */

import request from '@/config/axios'
import type {
  ReconciliationFileVO,
  ReconciliationFilePageReqVO,
  ReconciliationDiffVO,
  PageResult
} from './types'

/**
 * 获取对账文件分页列表
 * @param params 分页查询参数
 * @returns 对账文件分页数据
 */
export const getReconciliationFilePageApi = (
  params: ReconciliationFilePageReqVO
): Promise<PageResult<ReconciliationFileVO>> => {
  return request.get({ url: '/settle/reconciliation-file/page', params })
}

/**
 * 获取对账文件详情
 * @param id 文件ID
 * @returns 对账文件详情
 */
export const getReconciliationFileApi = (id: number): Promise<ReconciliationFileVO> => {
  return request.get({ url: `/settle/reconciliation-file/get?id=${id}` })
}

/**
 * 上传对账文件
 * @param file 对账文件
 * @param channelType 渠道类型
 * @param reconcileDate 对账日期
 * @returns 上传结果
 */
export const uploadReconciliationFileApi = (
  file: File,
  channelType: number,
  reconcileDate: string
): Promise<number> => {
  const formData = new FormData()
  formData.append('file', file)
  formData.append('channelType', channelType.toString())
  formData.append('reconcileDate', reconcileDate)

  return request.upload({
    url: '/settle/reconciliation-file/upload',
    data: formData
  })
}

/**
 * 处理对账文件
 * @param id 文件ID
 * @returns 处理结果
 */
export const processReconciliationFileApi = (id: number): Promise<boolean> => {
  return request.post({ url: `/settle/reconciliation-file/process?id=${id}` })
}

/**
 * 重新处理对账文件
 * @param id 文件ID
 * @returns 重新处理结果
 */
export const reprocessReconciliationFileApi = (id: number): Promise<boolean> => {
  return request.post({ url: `/settle/reconciliation-file/reprocess?id=${id}` })
}

/**
 * 删除对账文件
 * @param id 文件ID
 * @returns 删除结果
 */
export const deleteReconciliationFileApi = (id: number): Promise<boolean> => {
  return request.delete({ url: `/settle/reconciliation-file/delete?id=${id}` })
}

/**
 * 下载对账文件
 * @param id 文件ID
 * @returns 下载文件
 */
export const downloadReconciliationFileApi = (id: number): Promise<void> => {
  return request.download({
    url: `/settle/reconciliation-file/download?id=${id}`
  })
}

/**
 * 获取对账差异记录分页列表
 * @param params 分页查询参数
 * @returns 对账差异记录分页数据
 */
export const getReconciliationDiffPageApi = (params: {
  pageNo: number
  pageSize: number
  fileId?: number
  orderNo?: string
  diffType?: number
  handleStatus?: number
}): Promise<PageResult<ReconciliationDiffVO>> => {
  return request.get({ url: '/settle/reconciliation-diff/page', params })
}

/**
 * 获取对账差异记录详情
 * @param id 差异ID
 * @returns 对账差异记录详情
 */
export const getReconciliationDiffApi = (id: number): Promise<ReconciliationDiffVO> => {
  return request.get({ url: `/settle/reconciliation-diff/get?id=${id}` })
}

/**
 * 处理对账差异
 * @param id 差异ID
 * @param handleStatus 处理状态: 1-已处理 2-已忽略
 * @param handleRemark 处理备注
 * @returns 处理结果
 */
export const handleReconciliationDiffApi = (
  id: number,
  handleStatus: number,
  handleRemark?: string
): Promise<boolean> => {
  return request.put({
    url: '/settle/reconciliation-diff/handle',
    data: { id, handleStatus, handleRemark }
  })
}

/**
 * 批量处理对账差异
 * @param ids 差异ID列表
 * @param handleStatus 处理状态: 1-已处理 2-已忽略
 * @param handleRemark 处理备注
 * @returns 批量处理结果
 */
export const batchHandleReconciliationDiffApi = (
  ids: number[],
  handleStatus: number,
  handleRemark?: string
): Promise<boolean> => {
  return request.put({
    url: '/settle/reconciliation-diff/batch-handle',
    data: { ids, handleStatus, handleRemark }
  })
}

/**
 * 获取对账统计数据
 * @param params 查询参数
 * @returns 统计数据
 */
export const getReconciliationStatisticsApi = (params: {
  channelType?: number
  reconcileDate?: [string, string]
}): Promise<{
  totalFiles: number // 总文件数
  processedFiles: number // 已处理文件数
  pendingFiles: number // 待处理文件数
  failedFiles: number // 处理失败文件数
  totalDiffs: number // 总差异数
  handledDiffs: number // 已处理差异数
  pendingDiffs: number // 待处理差异数
  ignoredDiffs: number // 已忽略差异数
}> => {
  return request.get({ url: '/settle/reconciliation-file/statistics', params })
}

/**
 * 导出对账差异记录
 * @param params 查询参数
 * @returns 导出文件
 */
export const exportReconciliationDiffApi = (params: {
  fileId?: number
  orderNo?: string
  diffType?: number
  handleStatus?: number
}): Promise<void> => {
  return request.download({
    url: '/settle/reconciliation-diff/export',
    params
  })
}
